import { createMessage } from '$components/basic/message';

export function price_filter(value: number | string) {
	console.log(value);
	const v = Number(value);
	if (isNaN(v)) {
		return 0.0;
	}

	return v.toFixed(2);
}

export function price_transform(value: number | string) {
	const v = Number(value);
	if (isNaN(v)) {
		// createMessage({ type: 'danger', message: 'please enter a valid number' });
		return 0.0;
	}
	if (typeof value === 'string') {
		return Number(value)
			.toFixed(2)
			.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
	} else {
		return value.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
	}
}

export function input_quantity_filter(
	value: number | string,
	max?: number,
	min?: number
): string | number {
	// 判断value是否是整数
	if (isNaN(Number(value)) || Number(value) < 0) {
		createMessage({ type: 'danger', message: 'please enter a valid number' });
		return 0;
	}

	const result = Number(value);
	if (max && !isNaN(result) && result > max) {
		return max.toFixed(0);
	}

	if (min && !isNaN(result) && result < min) {
		return min.toFixed(0);
	}

	return result.toFixed(0);
}
